//
//  AfanNetworkTool.swift
//  weibo
//
//  Created by afan on 16/3/25.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit
import AFNetworking

class AfanNetworkTool: NSObject {
    
    
    ///网络单例
    static let sharedInstance : AfanNetworkTool = AfanNetworkTool()
    
    let afnManager: AFHTTPSessionManager = AFHTTPSessionManager(baseURL: AfanBaseURL)
    
    /// 自己来封装GET请求
    func requestGET(URLString: String, parameters: AnyObject?, requestCallback: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        
        afnManager.GET(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            // 网络请求回来的都是JSON格式
            let dict = result as? [String: AnyObject]
            
            requestCallback(result: dict, error: nil)
            
            }) { (_, error) -> Void in
                requestCallback(result: nil, error: error)
        }
    }
    
    
    
    /// 自己来封装POST请求
    func requestPOST(URLString: String, parameters: AnyObject?, requestCallback: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        afnManager.POST(URLString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            // 网络请求回来的都是JSON格式
            let dict = result as? [String: AnyObject]
            requestCallback(result: dict, error: nil)
            }) { (_, error) -> Void in
                requestCallback(result: nil, error: error)
        }
    }
    
    
    
    
    
    
    
    //把创建办法私有化.创建单例完成
    private override init() {
        super.init()
        
        //设置 AFN 支持 "text/plain"
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }
    
}
